package com.tianshi.yingxue.controller;

import com.tianshi.yingxue.dto.PageDTO;
import com.tianshi.yingxue.service.UserService;
import com.tianshi.yingxue.vo.UserVO;
import com.tianshi.yingxue.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/queryAllPage")
    @ResponseBody
    public UserVO queryAllPage(@RequestBody PageDTO pageDTO) {
        return userService.queryPage(pageDTO.getPage(), pageDTO.getPageSize());
    }

    @RequestMapping("/uploadHeadImg")
    @ResponseBody
    public Map<String, String> uploadHeadImg(MultipartFile headImg, String oldImg) {
        Map<String, String> map = new HashMap<>();
        String path = null;
        try {
            path = userService.uploadHeadImg(headImg, oldImg);
            System.out.println(path);
        } catch (IOException e) {
            map.put("message", "上传失败");
            throw new RuntimeException(e);
        }
        map.put("fileName", path);
        map.put("message", "上传成功");
        return map;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, String> addUser(@RequestBody User user) {
        Map<String, String> map = new HashMap<>();
        try {
            userService.addUser(user);
            map.put("message", "新增成功");
        } catch (Exception e) {
            map.put("message", "新增失败");
            throw new RuntimeException(e);
        }
        return map;
    }

    @GetMapping("queryById")
    @ResponseBody
    public User queryById(int id) {
        return userService.queryById(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, String> updateUser(@RequestBody User user) {
        Map<String, String> map = new HashMap<>();
        try {
            userService.updateUser(user);
            map.put("message", "修改成功");
        } catch (Exception e) {
            map.put("message", "修改失败");
            throw new RuntimeException(e);
        }
        return map;
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String, String> deleteUser(@RequestBody Map<String, Integer> request) {
        Integer id = request.get("id");
        Map<String, String> result = new HashMap<>();
        try {
            userService.deleteUser(id);
            result.put("status", "success");
            result.put("message", "用户删除成功");
        } catch (Exception e) {
            result.put("status", "error");
            result.put("message", "用户删除失败: " + e.getMessage());
            throw new RuntimeException(e);
        }
        return result;
    }
    @PostMapping("/exportExcel")
    public void exportUserExcel(HttpServletResponse response) {
        try {
            userService.exportUserExcel(response);
        } catch (Exception e) {
            throw new RuntimeException("导出失败", e);
        }
    }
}
